﻿using IOA.MES.Business;
using IOA.MES.ClientApi.Models.Request.Common;
using IOA.MES.ClientApi.Models.Response.Common;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Web.Http;

namespace IOA.MES.ClientApi.Controllers
{
    public class CommonController : ApiController
    {
        /// <summary>
        /// 查询设备类型
        /// </summary>
        [HttpPost]
        public ResultJson<QueryMachineTypesResponse> QueryMachineTypes([FromBody] QueryMachineTypesRequest request)
        {
            var machineTypes = new MachineMgr().QueryMachineTypes();
            var total = 0;
            var workshops = new WorkshopMgr().QueryWorkshops(1, 0, out total, null, "");
            var machines = new MachineMgr().QueryWorkshopMachines(request.WorkshopCode);
            return new ResultJson<QueryMachineTypesResponse>(new QueryMachineTypesResponse
            {
                MachineTypes = machineTypes,
                Workshops = workshops.Select(_ => new Workshop
                {
                    WorkshopCode = _.WorkshopCode,
                    WorkshopName = _.WorkshopName,
                    Status = _.Status
                }).ToList(),
                Machines = machines.Select(_ => new SimpleMachine
                {
                    MachineId = _.PkId,
                    MachineType = _.MachineType,
                    MachineName = _.MachineName,
                    WorkshopCode = _.WorkshopCode,
                    WorkshopName = _.WorkshopName,
                    FileType = new List<string> { "众森" }.Contains(_.Manufacturers, StringComparer.CurrentCultureIgnoreCase) ? 0 : 1
                }).ToList()
            });
        }

        /// <summary>
        /// 获取车间对象
        /// </summary>
        [HttpPost]
        public ResultJson<GetWorkshopResponse> GetWorkshop([FromBody] GetWorkshopRequest request)
        {
            var workshop = new WorkshopMgr().GetWorkshop(request.WorkshopCode);

            return new ResultJson<GetWorkshopResponse>(new GetWorkshopResponse
            {
                Workshop = workshop == null ? null : new Models.Response.Common.Workshop
                {
                    WorkshopCode = workshop.WorkshopCode,
                    WorkshopName = workshop.WorkshopName,
                    Status = workshop.Status,
                    CNStatus = workshop.CNStatus
                }
            });
        }

        /// <summary>
        /// 检测更新
        /// </summary>
        [HttpPost]
        public ResultJson<CheckUpgradeResponse> CheckUpgrade([FromBody] CheckUpgradeRequest request)
        {
            var packPath = $"{ConfigurationManager.AppSettings["UpgradePackLocation"].ToString()}\\{request.AppName}";
            if (!Directory.Exists(packPath))
            {
                return new ResultJson<CheckUpgradeResponse>(new CheckUpgradeResponse { HasUpgrade = false });
            }

            var lastPack = Directory.GetFiles(packPath).OrderByDescending(_ => _).FirstOrDefault();
            if (string.IsNullOrWhiteSpace(lastPack))
            {
                return new ResultJson<CheckUpgradeResponse>(new CheckUpgradeResponse { HasUpgrade = false });
            }
            if (Path.GetFileNameWithoutExtension(lastPack).CompareTo(request.CurrentVersion) <= 0)
            {
                return new ResultJson<CheckUpgradeResponse>(new CheckUpgradeResponse { HasUpgrade = false });
            }

            return new ResultJson<CheckUpgradeResponse>(new CheckUpgradeResponse { HasUpgrade = true, PackageUrl = $"{ConfigurationManager.AppSettings["UpgradePackRoot"].ToString()}/{request.AppName}/{Path.GetFileName(lastPack)}" });
        }
    }
}
